package com.zx.link.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zx.link.entity.Link;
import com.zx.link.utils.DBUtil;

@Repository("linkDao")
public class LinkDao {
	
	@Autowired
	private DBUtil dbUtil;
	
	/**
	 * 查询没有被删除的
	 */
	public List<Link> getAll(){
		List<Link> find = this.dbUtil.find(Link.class, "select id,name,tid,url,isRemove,decription,userName,password from link where isRemove=0");
		return find;
	}
	
	/**
	 * 查询所有被删除的
	 */
	public List<Link> getAllDelete(){
		List<Link> find = this.dbUtil.find(Link.class, "select id,name,tid,url,isRemove,decription,userName,password from link where isRemove=1");
		return find;
	}
	
	public Link getById(Integer id){
		List<Link> find = this.dbUtil.find(Link.class, "select id,name,icon,tid,url,isRemove,decription,userName,password from link where id=?",id);
		if(find.size() > 0 ){
			return find.get(0);
		}
		return null;
	}
	
	/**
	 * 查询图片
	 * @param id
	 * @return
	 */
	public byte[] getIconById(Integer id){
		List<Link> find = this.dbUtil.find(Link.class, "select id,name,icon,tid,url,isRemove,decription,userName,password from link where id=?",id);
		return find.get(0).getIcon();
	}
	
	/**
	 * @Title: 增加
	 * @param link
	 * @return
	 * @author: zhangx
	 * @date: 2017年3月1日 下午12:08:45
	 * @version v1.0
	 */
	public boolean insert(Link link){
		String sql = "insert into link(name,tid,icon,url,isRemove,decription,userName,password) values(?,?,?,?,?,?,?,?)";
		
		Object[] args = {link.getName(), link.getTid(), link.getIcon(), link.getUrl(), 
				0,link.getDecription(),link.getUserName(),link.getPassword()};
		
		int i = this.dbUtil.update(sql,args);
		
		if(i == 1) {
			return true;
		} 
		return false;
		
	}
	
	/**
	 * @Title: 修改
	 * @param link
	 * @return
	 * @author: zhangx
	 * @date: 2017年3月1日 下午12:09:02
	 * @version v1.0
	 */
	public boolean update(Link link){
		String sql = "update link set name=?,tid=?,icon=?,url=?,isRemove=?,decription=?,userName=?,password=? where id=?";
		
		Object[] args = {link.getName(), link.getTid(), link.getIcon(), link.getUrl(), 
				link.getIsRemove(),link.getDecription(),link.getUserName(),link.getPassword(), link.getId()};
		
		int i = this.dbUtil.update(sql,args);
		
		if(i == 1) {
			return true;
		} 
		return false;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public boolean delete(Integer id) {
		String sql = "delete from link where id=?";
		
		int i = this.dbUtil.update(sql,id);
		
		if(i == 1) {
			return true;
		} 
		return false;
	}
}
